package com.alisoft.nano.seda.notification;

import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;

import com.alisoft.nano.seda.Event;
import com.alisoft.nano.seda.Stage;

@SuppressWarnings("serial")
public abstract class StageNotificationListener<T> implements
		NotificationListener, NotificationFilter {
	
	@SuppressWarnings("unchecked")
	public void handleNotification(Notification notification, Object handback) {		
		StageNotification<T> stageNotification = (StageNotification<T>) notification;
		if (StageNotification.PREPOST.equals(stageNotification.getType())) {
			onPrePost(stageNotification.getEvent());
		} else if (StageNotification.ACCEPTED.equals(stageNotification.getType())) {
			onAccepted(stageNotification.getEvent());
		} else if (StageNotification.PRERUN.equals(stageNotification.getType())) {
			onPreRun(stageNotification.getEvent());
		} else if (StageNotification.DONE.equals(stageNotification.getType())) {
			onDone(stageNotification.getEvent());
		} else if (StageNotification.STARTED.equals(stageNotification.getType())) {
			onStarted(stageNotification.getStage());
		} else if (StageNotification.STOPPED.equals(stageNotification.getType())) {
			onStopped(stageNotification.getStage());
		}
		
	}
	
	protected void onStopped(Stage<T> stage) {
	}

	protected void onStarted(Stage<T> stage) {
	}

	protected void onDone(Event<T> event) {
	}

	protected void onPreRun(Event<T> event) {
	}

	protected void onAccepted(Event<T> event) {
	}

	protected void onPrePost(Event<T> event) {
	}

	public boolean isNotificationEnabled(Notification notification) {
		return StageNotification.class.isAssignableFrom(notification.getClass());
	}
	
	
}
